package com.jz.community.moduleshoppingguide.nearshop.ui.view;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jz.community.basecomm.utils.BaseImageLoaderUtils;
import com.jz.community.basecomm.utils.RxDataTool;
import com.jz.community.commview.view.RelativePopupWindow;
import com.jz.community.moduleshoppingguide.R;
import com.jz.community.moduleshoppingguide.nearshop.bean.ShopInfoBean;
import com.jz.community.moduleshoppingguide.nearshop.ui.activity.NearShopInfoLocationActivity;
import com.jz.community.moduleshoppingguide.nearshop.ui.adapter.ShopInfoImageListAdapter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by Andy Huang on 2018/3/19.
 *
 * @description:
 */

public class NearShopInfoPopWindow extends RelativePopupWindow {


    private View view, pop_near_shop_shade;
    private Context context;
    private ShopInfoImageListAdapter shopInfoImageListAdapter;
    private RecyclerView recyclerView;
    private TextView tvShopInfoExplain, shopInfoSendType, shopInfoShopPhone, shopInfoShopOpeningHours, shopInfoShopAddress;
    private ImageView ivCloseShopInfoPopWindow;
    private List<String> Img_arr = new ArrayList<>();
    private LinearLayout llShopInfoLocation, llShopInfoPhone;
    private RelativeLayout rlShopInfoExplain;
    private boolean popIsShow = false;//Pop是否展开
    private Dialog dialog;
    private ImageView iv_shop_info_image;
    private ImageView mImageView;
    ShopInfoBean shopInfoBean;


    public NearShopInfoPopWindow(final Context context, ShopInfoBean shopInfoBean) {
        super(context);
        this.context = context;
        this.shopInfoBean = shopInfoBean;
        this.view = LayoutInflater.from(context).inflate(R.layout.shop_info_pop_window, null);
        recyclerView = view.findViewById(R.id.shop_info_picture_recycler_view);
        tvShopInfoExplain = view.findViewById(R.id.tv_shop_info_explain);
        shopInfoSendType = view.findViewById(R.id.shop_info_send_type);
        shopInfoShopPhone = view.findViewById(R.id.shop_info_shop_phone);
        shopInfoShopOpeningHours = view.findViewById(R.id.shop_info_shop_opening_hours);
        shopInfoShopAddress = view.findViewById(R.id.shop_info_shop_address);
        ivCloseShopInfoPopWindow = view.findViewById(R.id.iv_close_shop_info_pop_window);
        llShopInfoLocation = view.findViewById(R.id.ll_shop_info_location);
        llShopInfoPhone = view.findViewById(R.id.ll_shop_info_phone);
        rlShopInfoExplain = view.findViewById(R.id.rl_shop_info_explain);
        pop_near_shop_shade = view.findViewById(R.id.pop_near_shop_shade);
        initRecyclerView();
        initData();
        /* 设置弹出窗口特征 */
        // 设置视图
        setContentView(view);
        // 设置弹出窗体的宽和高
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);//(width / 7) * 6//ViewGroup.LayoutParams.WRAP_CONTENT
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        // 设置弹出窗体可点击
        this.setFocusable(true);
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0000000000);
        // 设置弹出窗体的背景
        this.setBackgroundDrawable(dw);
        llShopInfoPhone.setOnClickListener(v -> {
            //跳转到拨号页面
            Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + shopInfoBean.getContact_tel()));
            context.startActivity(intent);
        });
        ivCloseShopInfoPopWindow.setOnClickListener(v -> {
            dismiss();
            popIsShow = false;
        });
        pop_near_shop_shade.setOnClickListener(view -> {
            dismiss();
            popIsShow = false;
        });
        llShopInfoLocation.setOnClickListener(v -> {
            //跳转到地图页
            if (!RxDataTool.isNullString(shopInfoBean.getLongitude()) && !RxDataTool.isNullString(shopInfoBean.getLatitude())) {
                Intent intent = new Intent(context, NearShopInfoLocationActivity.class);
                intent.putExtra("lat", shopInfoBean.getLatitude());
                intent.putExtra("lon", shopInfoBean.getLongitude());
                intent.putExtra("near_shop_name", shopInfoBean.getName());
                intent.putExtra("near_shop_address", shopInfoBean.getAddress());
                context.startActivity(intent);
            }
        });
        popIsShow = true;
    }


    private void initRecyclerView() {
        LinearLayoutManager main_layoutManager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
        recyclerView.setHasFixedSize(true);
        recyclerView.setFocusable(false);
        recyclerView.setLayoutManager(main_layoutManager);
        shopInfoImageListAdapter = new ShopInfoImageListAdapter(R.layout.module_shoppingguide_item_shop_info_shop_image_list, Img_arr);
        recyclerView.setAdapter(shopInfoImageListAdapter);
    }


    private void initData() {
        tvShopInfoExplain.setText(shopInfoBean.getWelcome());
        String send_type = shopInfoBean.getSend_type();
        if ("2".equals(send_type)) {
            shopInfoSendType.setText("仅支持到店自提");
        } else if ("1".equals(send_type)) {
            shopInfoSendType.setText("仅支持配送");
        } else if ("0".equals(send_type)) {
            shopInfoSendType.setText("支持配送与自提");
        } else {
            shopInfoSendType.setText("仅支持到店自提");
        }
        shopInfoShopAddress.setText(shopInfoBean.getAddress());
        shopInfoShopPhone.setText(shopInfoBean.getContact_tel());
        if (!TextUtils.isEmpty(shopInfoBean.getBusinessStartHours()) && !TextUtils.isEmpty(shopInfoBean.getBusinessEndHours())) {
            shopInfoShopOpeningHours.setText(shopInfoBean.getBusinessStartHours() + "--" + shopInfoBean.getBusinessEndHours());
        }
        if (!TextUtils.isEmpty(shopInfoBean.getImg_arr())) {
            if (shopInfoBean.getImg_arr().contains(";")) {
                String[] images = shopInfoBean.getImg_arr().split(";");
                Img_arr = Arrays.asList(images);
                shopInfoImageListAdapter.setNewData(Img_arr);
            } else {
                Img_arr.add(shopInfoBean.getImg_arr());
                shopInfoImageListAdapter.setNewData(Img_arr);
            }
            shopInfoImageListAdapter.setOnItemClickListener((adapter, view, position) -> smallImgClick(view, position));
        }
    }

    public void smallImgClick(View v, int position) {
        // 全屏显示的方法
        final Dialog dialog = new Dialog(context, android.R.style.Theme_Black_NoTitleBar_Fullscreen);//android.R.style.Theme_Black_NoTitleBar_Fullscreen
        ImageView imgView = getView(position);
        dialog.setContentView(imgView);
        dialog.show();
        // 点击图片消失
        imgView.setOnClickListener(v1 -> dialog.dismiss());
    }


    private ImageView getView(int position) {
        ImageView imgView = new ImageView(context);
        imgView.setLayoutParams(new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT));
        BaseImageLoaderUtils.getInstance().loadImage(context, imgView, Img_arr.get(position), R.mipmap.default_img_1_1);
        return imgView;
    }

}
